
/**		
 * 
 *	uk.co.lmfm.guppy.view.components.project.ProjectSettingsView
 *	
 *	@version 1.00 | Jan 22, 2010
 *	@author Justin Windle
 *  
 **/
 
package uk.co.lmfm.guppy.view.components.project 
{
	import uk.co.lmfm.guppy.model.vo.FlashPlayerVO;
	import uk.co.lmfm.guppy.model.vo.WorkspaceVO;
	import uk.co.lmfm.guppy.view.components.core.IViewComponent;
	import uk.co.lmfm.guppy.view.components.core.form.ComboBox;
	import uk.co.lmfm.guppy.view.components.core.form.InputButtonField;
	import uk.co.lmfm.guppy.view.components.core.form.LabeledFormField;

	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filesystem.File;

	/**
	 * ProjectSettingsView
	 */
	public class ProjectSettingsView extends ProjectUIPanel implements IViewComponent 
	{
		//	----------------------------------------------------------------
		//	PRIVATE MEMBERS
		//	----------------------------------------------------------------

		private var _rootPackage : LabeledFormField = new LabeledFormField();
		private var _projectName : LabeledFormField = new LabeledFormField();
		private var _projectClass : LabeledFormField = new LabeledFormField();		private var _folderInput : InputButtonField = new InputButtonField();
		private var _workspace : ComboBox = new ComboBox();		private var _player : ComboBox = new ComboBox();
		private var _folder : File = new File();

		//	----------------------------------------------------------------
		//	CONSTRUCTOR
		//	----------------------------------------------------------------

		public function ProjectSettingsView()
		{
			super();
			
			_projectName.label.text = "Project name";
			_rootPackage.label.text = "Root package (optional)";			_projectClass.label.text = "Main Class";
			
			_workspace.label = "Workspace";
			_workspace.width = 160;
			
			_folderInput.label.text = "Folder (optional)";
			_folderInput.button.label = "Browse";
			
			_player.label = "Player / AIR Version";

			_rootPackage.width = 160;
			_projectName.width = 160;
			
			_rootPackage.input.height = _folderInput.button.height;
			_projectName.input.height = _folderInput.button.height;			_projectClass.input.height = _folderInput.button.height;
			
			_rootPackage.input.restrict = "a-zA-Z0-9.";
			
			_projectClass.input.text = "Application.as";
			_projectClass.input.restrict = "a-zA-Z0-9";
			
			_rootPackage.input.addEventListener(Event.CHANGE, onTextInputChange);			_projectClass.input.addEventListener(Event.CHANGE, onTextInputChange);
			_folderInput.button.addEventListener(MouseEvent.CLICK, onBrowseForFolder);
			
			_folderInput.button.width = 45;

			addChild(_rootPackage);
			addChild(_projectName);			addChild(_projectClass);			addChild(_workspace);			addChild(_player);
			addChild(_folderInput);
		}

		//	----------------------------------------------------------------
		//	PROTECTED METHODS
		//	----------------------------------------------------------------

		public function clearWorkspaces() : void
		{
			_workspace.clear();
		}

		public function clearPlayers() : void
		{
			_player.clear();
		}

		public function addPlayer(player : FlashPlayerVO) : void
		{
			if(player.isDefault) _player.currentItem = _player.addItem(player.name, player);
			else _player.addItem(player.name, player);
		}

		public function addWorkspace(workspace : WorkspaceVO) : void
		{
			_workspace.addItem(workspace.name, workspace);
		}

		//	----------------------------------------------------------------
		//	PROTECTED METHODS
		//	----------------------------------------------------------------

		override protected function resize() : void
		{
			super.resize();
			
			var w : int = stage.stageWidth;
			var w3 : int = (w - (4 * 10)) / 3;
			
			_projectName.width = _rootPackage.width = _projectClass.width = w3;
			_workspace.width = _folderInput.width = w3;

			_projectName.x = _projectName.y = 10;
			
			_rootPackage.x = _projectName.x + _projectName.width + 10;
			_rootPackage.y = _projectName.y;
			
			_projectClass.x = _rootPackage.x + _rootPackage.width + 10;
			_projectClass.y = _projectName.y;

			_workspace.x = _projectName.x;
			_workspace.y = _projectName.y + _projectName.height + 5;
			
			_folderInput.x = _workspace.x + _workspace.width + 10;
			_folderInput.y = _workspace.y;
			
			_player.x = _folderInput.x + _folderInput.width + 10;
			_player.y = _folderInput.y;
			
			_player.width = w3;

			height = _projectName.y + _folderInput.y + _folderInput.height;
		}

		//	----------------------------------------------------------------
		//	PRIVATE METHODS
		//	----------------------------------------------------------------

		private function toCamelCase(str : String) : String
		{
			var r : RegExp = /(\b\w)/g;
			var o : Object = r.exec(str);

			while(o)
			{
				var i : int = o.index;
				str = str.substr(0, i) + String(o[0]).toUpperCase() + str.substr(i + 1, str.length);
				o = r.exec(str);
			}

			return str.replace(/\s/g, '');
		}

		private function validatePackageName(str : String) : String
		{
			str = str.replace(/^\./g, '');
			str = str.replace(/\.{2,}/g, '.');
	
			return str;
		}

		private function validateClassName(str : String) : String
		{
			str = str.replace(/^[0-9]|.as$/, '');
			str = str.substr(0, 1).toUpperCase() + str.substr(1);
			str = str.replace(/\W/g, '') + '.as';
	
			return str;
		}

		//	----------------------------------------------------------------
		//	EVENT HANDLERS
		//	----------------------------------------------------------------

		private function onBrowseForFolder(event : MouseEvent) : void
		{
			_folder.nativePath = workspace.location.nativePath;
			_folder.addEventListener(Event.SELECT, onFolderSelected);
			_folder.browseForDirectory("Select a folder for your project");
		}

		private function onFolderSelected(event : Event) : void
		{
			var pattern : String = "(" + workspace.location.nativePath + ")/?(.*)";
			var regex : RegExp = new RegExp(pattern);
			
			_folderInput.input.text = _folder.nativePath.replace(regex, '/$2');
		}

		private function onTextInputChange(event : Event) : void
		{
			switch(event.currentTarget)
			{
				case _rootPackage.input :
				
					_rootPackage.input.text = validatePackageName(_rootPackage.input.text);
				 
					break;
					
				case _projectClass.input :
				
					_projectClass.input.text = validateClassName(_projectClass.input.text);
				 
					break;
			}
		}

		//	----------------------------------------------------------------
		//	PUBLIC ACCESSORS
		//	----------------------------------------------------------------

		public function get projectName() : String
		{
			return _projectName.value;
		}

		public function get rootPackage() : String
		{
			return _rootPackage.value.replace(/\.$/, '');
		}

		public function get workspace() : WorkspaceVO
		{
			return _workspace.currentItem.data as WorkspaceVO;
		}

		public function get mainClass() : String
		{
			return validateClassName(_projectClass.input.text);
		}

		public function get playerVersion() : FlashPlayerVO
		{
			return _player.currentItem.data as FlashPlayerVO;
		}

		public function get folder() : File
		{
			trace(_folderInput.input.text);
			
			if(_folderInput.input.text.length > 1)
			{
				try
				{
					_folder.nativePath;
				}
				catch(error : Error)
				{
					return workspace.location.resolvePath(projectName);
				}
				
				return _folder;
			}
			else
			{
				return workspace.location.resolvePath(projectName);
			}
		}
	}
}
